<?php
/**
 * This file contains the class AlertsDB which provides
 * all of the database requirements for the alerts page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides access to the database for alerts
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class RecentListingsDB
{
    /**
      * Provides access to the database
     *
     * @var Db
     */
    private $_db;
    /**
     * Create a new instance of this class
     * 
     * @return void
     * @param Db &$db
     */
    function __construct(&$db)
    {
        $this->_db = $db;
    }
    /**
     * Escape the provided value
     *
     * @return string
     * @param mixed $value
     */
    function escape($value)
    {
        $db = $this->_db;
        return $db->escape($value);
    }
    /**
     * Get people with remembrance pages opened OR modified today
     *
     * @return array[][string]string
     * @param int $limit
     */
    function getPeople($limit=999)
    {
        $db = $this->_db;
        $people = array();
        $limit = intval($limit);
        $sql = <<< EOT
            SELECT DISTINCT Person.Id, FirstNames, Surname
            FROM Person
                LEFT OUTER JOIN PublicationPerson
                    ON PublicationPerson.PersonId = Person.Id
            WHERE
                Person.AddedOn > DATE_SUB(NOW(),INTERVAL 1 DAY)
             OR PublicationPerson.AddedOn > DATE_SUB(NOW(),INTERVAL 1 DAY)
            ORDER BY Person.Surname ASC
EOT;
        if(!empty($limit)||$limit==999){
            $sql = $sql." LIMIT 0,".$limit;
        }
        $result = $db->query($sql);
        if ($db->rowCount($result) > 0) {
            while (TRUE) {
                $row = $db->fetch($result);
                if (empty($row)) {
                    break;
                }
                $id = intval($row['Id']);
                $person = array(
                    'surname' => $db->unescape($row['Surname']),
                    'firstNames' => $db->unescape($row['FirstNames'])
                );
                if ($person['surname'] !== 'ERROR') {
                    $people[$id] = $person;
                }
            }
        }
        return $people;
    }
}
?>